package zjc.bookkeeping.minepage.bClassificationkeywords;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.List;

import zjc.bookkeeping.R;
import zjc.bookkeeping.activity.MainActivity;
import zjc.bookkeeping.db.ConsumptionClassBean;
import zjc.bookkeeping.db.DBManager;

public class LabelActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_label);

        initSpinner1();
        initSpinner2();
    }
    //返回上一页
    public void back(View v){
        finish();
//        // Intent 去设置要跳转的页面
//        Intent intent = new Intent(this, ClassificationkeywordsMainActivity.class);
//        // 进行跳转
//        startActivity(intent);
    }
    //查询数据
    private String[] classname(int j){
        List<ConsumptionClassBean> list = DBManager.getConsumptionClass(j);
        int k = 0 ;
        String[] text = new String[list.size()];
        k = list.size();
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++) {
            if (j==0&&i==0){
                text[i] = "支出";
            }else if(j==1&&i==0){
                text[i] = "收入";
            }else  {
                String name = list.get(i).getConsumption_class_name();
                text[i] = name;
                System.out.println(i + "账户:" + list.get(i).getConsumption_class_kind() + list.get(i).getConsumption_class_name() + list.get(i).getConsumption_class_image());
            }
        }
        return text;
    }

//下拉框
    private void initSpinner1(){
        String[] classArray1 = classname(0);
        //声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,R.layout.b_item_select,classArray1);
        //设置数组适配器的布局样式
        starAdapter.setDropDownViewResource(R.layout.b_item_dropdown);
        //从布局文件中获取名叫sp_dialog的下拉框
        Spinner sp = findViewById(R.id.spinner1);
        //设置下拉框的标题，不设置就没有难看的标题了
        sp.setPrompt("请选择分类");
        //设置下拉框的数组适配器
        sp.setAdapter(starAdapter);
        //设置下拉框默认的显示第一项
        sp.setSelection(0);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sp.setOnItemSelectedListener(new MySelectedListener1());
    }
    class MySelectedListener1 implements AdapterView.OnItemSelectedListener{
        String[] classArray1 = classname(0);
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(LabelActivity.this,"您选择的是："+classArray1[i],Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }

    private void initSpinner2(){
        String[] classArray2 = classname(1);
        //声明一个下拉列表的数组适配器
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this,R.layout.b_item_select,classArray2);
        //设置数组适配器的布局样式
        starAdapter.setDropDownViewResource(R.layout.b_item_dropdown);
        //从布局文件中获取名叫sp_dialog的下拉框
        Spinner sp = findViewById(R.id.spinner2);
        //设置下拉框的标题，不设置就没有难看的标题了
        sp.setPrompt("请选择分类");
        //设置下拉框的数组适配器
        sp.setAdapter(starAdapter);
        //设置下拉框默认的显示第一项
        sp.setSelection(0);
        //给下拉框设置选择监听器，一旦用户选中某一项，就触发监听器的onItemSelected方法
        sp.setOnItemSelectedListener(new MySelectedListener2());
    }
    class MySelectedListener2 implements AdapterView.OnItemSelectedListener{
        String[] classArray2 = classname(1);
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(LabelActivity.this,"您选择的是："+classArray2[i],Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    }
}